/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dwr.comet;

import org.apache.commons.httpclient.methods.PostMethod;
import org.orbeon.oxf.pipeline.api.PipelineContext;
import org.orbeon.oxf.processor.SimpleProcessor;
import org.orbeon.oxf.processor.ProcessorInputOutputInfo;
import org.xml.sax.ContentHandler;
import org.xml.sax.helpers.AttributesImpl;
import org.dom4j.Document;
import org.orbeon.oxf.util.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.xml.sax.SAXException;

/**
 *
 */
public class PageRewriter extends SimpleProcessor {
 

    public PageRewriter() {
        addInputInfo(new ProcessorInputOutputInfo("data"));
        addInputInfo(new ProcessorInputOutputInfo("config"));
        addOutputInfo(new ProcessorInputOutputInfo("data"));
    }

    public void generateData(PipelineContext context, ContentHandler contentHandler) throws SAXException {
        HttpClient client = new HttpClient();
        Document body = readInputAsDOM4J(context, "data");
        Document config = readInputAsDOM4J(context, "config");
        PostMethod post = new PostMethod("http://localhost:8080" + config.selectObject("string(/config/request-uri)").toString().replace("comet", "dwr"));
        post.setRequestHeader("cookie", (String) config.selectObject("string(/config/headers[name='cookie']/value)"));
        post.setRequestHeader("content-type", "text/plain");
        String group = (String) config.selectObject("string(/config/groups/@current)");
        String user = (String) config.selectObject("string(/config/username)");
        String referer = (String) config.selectObject("string(/config/headers[name='referer']/value)");
        String complete = (String) body.selectObject("string(/body)");
        post.setRequestHeader("username", user);
        post.setRequestHeader("group", group);
        post.setRequestHeader("referer", referer + group + "/");

        complete = new String(Base64.decode(complete));

        String output = complete.replace("%2Fportal%2Fmain%2F", "%2Fportal%2Fmain%2F" + group + "%2F");

        post.setRequestBody(output);

        contentHandler.startDocument();
        contentHandler.startElement("", "body", "body", new AttributesImpl());
        String response = null;

        try {

            int responseCode = client.executeMethod(post);
            if (responseCode != -1) {
                response = post.getResponseBodyAsString();
                String[] lignes = response.split("\n");
                String r = "";
                for (String l : lignes){
                    if (!l.contains("group:\"" + group) && l.contains("group:\"")) {

                    }else
                        r += l + "\n";
                }
                contentHandler.characters(r.toCharArray(), 0, r.length());
            } else {
                response = "Communication error";
            }


        } catch (Exception e) {
            System.out.println("IOException");
        }


        contentHandler.endElement("", "body", "body");
        contentHandler.endDocument();

    }
}
